<template>
  <div>
    <el-card>
      <div slot="header">角色授权
        <!-- <el-button type="primary" size="small" @click="add">新增</el-button>
        <el-button type="danger" size="small">删除</el-button> -->
        <!-- <el-button type="primary" @click="addDialogShow()">新增角色</el-button>
        <el-button type="danger" @click="centerDialogVisible = true">删 除</el-button> -->
      </div>
      <!-- 提示部分 -->
      <!-- 主体部分的表格 -->
      <el-table stripe :data="tableData.row" ref="userRoles" style="width: 100%" :current-row-key="tableData.row.id">
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          type="index"
          label="编号"
          width="50"
        >
        </el-table-column>
        <el-table-column label="名称">
          <template slot-scope="slotProps">
            {{slotProps.row.name}}
          </template>
        </el-table-column>
        <el-table-column label="描述">
          <template slot-scope="slotProps">
            {{slotProps.row.des}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="slotProps">
            {{slotProps.row.createTime}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="slotProps">
            <el-button type="text" @click="editDialogById(slotProps.row.id)">授权</el-button>
            <el-button type="text" @click="updateDialog(slotProps.row.id)">属性编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 编辑权限的弹出框 -->
    <el-dialog
      title="权限编辑"
      :visible.sync="dialog.dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-tree
        :data="dialog.resources"
        :props="dialog.defaultProps"
        show-checkbox
        ref="tree"
        node-key="id"
        :default-checked-keys="dialog.defaultSelected"
        >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancleDialog">取 消</el-button>
        <el-button type="primary" @click="updateRoleResources">保存</el-button>
      </span>
    </el-dialog>
    <!-- 添加用户的弹出框 -->
    <el-dialog
      title="属性管理"
      :visible.sync="addDialog.dialogVisible"
      width="30%"
      :before-close="addDialogHandleClose">

      <el-form ref="formData" :model="addDialog.formData" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="addDialog.formData.name"></el-input>
        </el-form-item>
        
        <el-form-item label="描述">
          <el-input v-model="addDialog.formData.des" placeholder="角色责任描述"></el-input>
        </el-form-item>

         <el-form-item label="启用">
          <el-switch v-model="addDialog.formData.b_status"></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialog.dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole()">保存</el-button>
      </span>
    </el-dialog>
    <!-- 确认删除 -->
    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <span>确认删除吗?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRole(tableData)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
export default {
  created() {
    this.getList()
  },
  data() {
    return {
      page: {
        pageSize: 10,
        currentPage: 1
      },
      centerDialogVisible: false,
      nowRow: -1,
      tableData: {
        row: []
      },
      dialog: {
        dialogVisible: false,
        resources: [],
        defaultProps: {
          children: 'firstFloor',
          label: 'name'
        },
        defaultSelected: []
      },
      addDialog: {
        dialogVisible: false,
        formData: {
          b_status: true,
          id: -1
        }
      }
    }
  },
  methods: {
    getList() {
      return request.get('/ac/role', { params: {
        size: this.page.pageSize,
        current: this.page.currentPage
      }}).then(res => {
        this.tableData.row = res.content
      })
    },
    editDialogById(index) {
      this.nowRow = index
      this.dialog.dialogVisible = true
      request.get('/ac/resources/' + index, { params: {}}).then(res => {
        this.dialog.resources = res.content
        this.setCheckedNodes(this.dialog.resources)
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？').then(_ => {
        this.dialog.defaultSelected = []
        done()
      }).catch(_ => {})
    },
    setCheckedNodes(all) {
      all.forEach(i => {
        var a = false
        if (i['mstatus'].toString() !== '-1') {
          this.dialog.defaultSelected.push(i['id'])
        }
        if (i['status'].toString() === '0') {
          i.disabled = true
          a = true
        }
        if (i['firstFloor'].Length !== 0) {
          i['firstFloor'].forEach(o => {
            var b = false
            if (a) {
              o.disabled = true
            }
            if (o['mstatus'].toString() !== '-1') {
              this.dialog.defaultSelected.push(o['id'])
            }
            if (o['status'].toString() === '0') {
              o.disabled = true
              b = true
            }
            if (o['firstFloor'].Length !== 0) {
              o['firstFloor'].forEach(p => {
                if (b) {
                  p.disabled = true
                }
                if (p['mstatus'].toString() !== '-1') {
                  this.dialog.defaultSelected.push(p['id'])
                }
                if (i['status'].toString() === '0') {
                  p.disabled = true
                }
              })
            }
            b = false
          })
        }
        a = false
      })
    },
    updateRoleResources() {
      var that = this
      var formData = {
        ids: that.$refs.tree.getCheckedKeys(),
        roleId: that.nowRow
      }
      request.put('/ac/resources/resources', formData).then(_ => {

      }).catch(_ => {})
      this.dialog.dialogVisible = false
      this.dialog.defaultSelected = []
    },
    cancleDialog() {
      this.dialog.dialogVisible = false
      this.dialog.defaultSelected = []
    },
    // 新增角色dialog事件
    addDialogHandleClose() {
      this.addDialog.dialogVisible = false
    },
    addDialogShow() {
      this.addDialog.formData.id = -1
      this.addDialog.formData = {}
      this.addDialog.dialogVisible = true
    },
    addRole() {
      this.addDialog.formData.status = this.addDialog.formData.b_status ? 1 : 0
      request.post('/ac/role/role', this.addDialog.formData).then(_ => {
        this.getList()
      }).catch(_ => {})
      this.addDialog.dialogVisible = false
    },
    deleteRole(row) {
      var rolesArr = this.$refs.userRoles.store.states.selection
      var idss = ''
      rolesArr.forEach(i => {
        idss = idss === '' ? idss + i['id'] : idss + ',' + i['id']
      })
      request.delete('/ac/role/ids', { params: {
        'ids': idss
      }}).then(_ => {
        console.log(_)
      }).catch(_ => {})
      this.centerDialogVisible = false
      this.getList()
    },
    updateDialog(index) {
      request.get('/ac/role/' + index).then(_ => {
        this.addDialog.dialogVisible = true
        this.addDialog.formData.name = _.content.name
        this.addDialog.formData.des = _.content.des
        this.addDialog.formData.b_status = _.content.status === 1
        this.addDialog.formData.id = _.content.id
      }).catch(_ => {})
    }
  }
}
</script>

<style scoped>
.point {
  width: 100%;
  height: 40px;
  margin-top: 8px;
  border-bottom: 1px solid #eee;
  box-shadow: none;
  font-size: 14px;
  text-align: rigth;
}
.point-text {
  margin-right: 14px;
  line-height: 40px;
  float: right;
}
</style>
